# 自定义中间类
from django.shortcuts import redirect
from django.urls import reverse
import re


class ShopMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        print('ShopMiddleware')

    def __call__(self, request):
        path = request.path
        print('url:', path)

        #判断管理后台是否登录
        urllist = ['/myadmin/login','/myadmin/logout','/myadmin/dologin','/myadmin_verify']
        if re.match(r'^/myadmin', path) and (path not in urllist):
            if 'adminuser' not in request.session:
                return redirect(reverse("myadmin_login"))# 重定向到登录页

        response = self.get_response(request)

        return response